<template>
  <div class="app-container">

    <div style="display: flex;align-items: center;margin-bottom: 2vh">
      <el-upload
        action="/"
        ref="upload"
        accept=".txt"
        limit="3"
        multiple
        :show-file-list="false"
        :before-upload="beforeUpload"
      >
        <el-button type="primary" >上传文件</el-button>
      </el-upload>

      <el-select v-model="value" placeholder="类型选择" style="margin-left: 1vw" v-show="type==0">
        <el-option
          v-for="item in options"
          :key="item"
          :label="item"
          :value="item"></el-option>
      </el-select>
      <el-input v-show="type==1" v-model="value" placeholder="分类名称" style="width: 15vw;margin-left: 1vw"></el-input>

      <el-select v-model="type" placeholder="分类类型" style="margin-left: 1vw">
        <el-option
          v-for="item in types"
          :key="item.value"
          :label="item.label"
          :value="item.value"></el-option>
      </el-select>

    </div>

    <el-input type="textarea" :autosize="{minRows:5,maxRows:25}" v-model="text"></el-input>

    <div style="float: right;margin-top: 2vh">
      <el-button type="info" plain>测试分类</el-button>
      <el-button type="primary" @click="heandler(text)">训练</el-button>
    </div>


  </div>
</template>

<script>

import {train, saveCategory, getCategory} from "@/api/sys/train";

export default {
  data() {
    return {
      fileList: [],
      text: "",
      category: '',
      dialogVisible: false,
      options: [],
      value: '',
      types: [{
        value:0,
        label:'现有分类'
      },{
        value:1,
        label:'自定义分类'
      }],
      type:0,
    }
  },


  created() {
    getCategory().then(response => {
      this.options = response.data
    })
  },

  beforeCreate() {
    // 读取文件
    FileReader.prototype.reading = function ({encode} = pms) {
      let bytes = new Uint8Array(this.result);    //无符号整型数组
      let text = new TextDecoder(encode || 'UTF-8').decode(bytes);
      return text;
    };
    /* 重写readAsBinaryString函数 */
    FileReader.prototype.readAsBinaryString = function (f) {
      if (!this.onload)       //如果this未重写onload函数，则创建一个公共处理方式
        this.onload = e => {  //在this.onload函数中，完成公共处理
          let rs = this.reading();
          console.log(rs);
        };
      this.readAsArrayBuffer(f);  //内部会回调this.onload方法
    };
  },
  methods: {
    handleClose(done) {
      done()
    },
    addCategory() {
      saveCategory(this.category).then(response => {
        this.$message.success("添加分类成功")
      })
    },

    // 添加数据的处理
    heandler(text) {
      if (text != "" && this.value != "") {
        var value = this.value
        var data = {
          category:value,
          text:text
        }
        train(data).then(response => {
          this.$message.success("模型训练成功")
        });
      }
    },

    beforeUpload(file) {
      this.fileList = [file]
      // 读取数据
      this.read(file);
      return false
    },
    read(f) {
      let rd = new FileReader();
      rd.onload = e => {
        //this.readAsArrayBuffer函数内，会回调this.onload函数。在这里处理结果
        let cont = rd.reading({encode: 'UTF-8'});
        this.text = cont;
        console.log(cont);
        let formerData = this.textData;
        this.textData = formerData + "\n" + cont;
      };
      rd.readAsBinaryString(f);
    }
  },


};
</script>
